<?php
/**
 * Created by PhpStorm.
 * User: yyq
 * Date: 2018/8/22
 * Time: 17:29
 */

namespace Goods\Controller;


use Common\Controller\AdminbaseController;
use Common\Model\CategoryModel;
use Common\Model\CommonModel;

class CategoryController extends AdminbaseController
{
    private $category_model;

    public function __construct()
    {
        parent::__construct();
        $this->category_model   = new CategoryModel();
    }

    public function lists() {
        $where['if_show']   = 'yes';
        if(IS_POST) {
            $cate_name  = I('post.cate_name');
            if($cate_name) {
                $where['cate_name'] = ['like', '%'.$cate_name.'%'];
            }
        } else {
            $cate_name  = I('get.cate_name');
            if($cate_name) {
                $where['cate_name'] = ['like', '%'.$cate_name.'%'];
            }
        }


        $count = $this->category_model->where($where)->count();
        $page = $this->page($count, C("PAGE_NUMBER"));
        $result = $this->category_model
            ->where($where)
            ->limit($page->firstRow, $page->listRows)
            ->select();

        $para['list'] = $result;
        $para['Page'] = $page->show();
        $para['formget']    = I('');
        $this->assign($para);
        $this->display();
    }

    public function add() {
//            dump(I('post.'));exit;
        $post = I('post.');

        if($this->category_model->add($post)) {
            $this->success('操作成功', U('lists'));
        } else {
            $this->error('操作失败');
        }

    }

    public function edit() {
//      dump(I('post.'));exit;
        $post = I('post.');

        if($this->category_model->save($post)) {
            $this->success('操作成功', U('lists'));
        } else {
            $this->error('操作失败');
        }

    }

    public function del() {
        $cate_id    = I('get.cate_id');

        if($this->category_model->where(['cate_id' => $cate_id])->save(['if_show' => 'no'])) {
            $this->success();
        } else {
            $this->error('操作失败');
        }
    }
}